package org.zjx.app.action.association;

import lombok.Getter;
import org.zjx.analysis.analyze.association.AssociationPurchaseAnalysis;
import org.zjx.app.action.AbstractWindowAction;
import org.zjx.command.entity.Association;
import org.zjx.window.operation.WindowOperation;
import org.zjx.window.window.IWindow;

import java.awt.*;

public class AssociationPurchaseAction extends AbstractWindowAction {
    private AssociationPurchaseAnalysis associationPurchaseAnalysis;
    private final Association association;
    @Getter
    private boolean okBuy = false;

    public AssociationPurchaseAction(IWindow targetWindow, Association association) {
        super(targetWindow);
        this.association = association;
    }

    @Override
    public void open() {
    }

    @Override
    public void close() {
        analysis();
        if (!associationPurchaseAnalysis.isOpened()) return;

        Rectangle closeButton = associationPurchaseAnalysis.getCloseButtonPoint();
        targetWindow.actions().clickIn(closeButton).delay(300);
        targetWindow.flushScreenshot();
        close();
    }

    private void analysis() {
        associationPurchaseAnalysis = new AssociationPurchaseAnalysis(targetWindow);
    }

    @Override
    public void reSetWindow() {
        if (associationPurchaseAnalysis.isTransborder()) {
            Rectangle from = associationPurchaseAnalysis.conversion(associationPurchaseAnalysis.windowArea,associationPurchaseAnalysis.getRefImageArea());
            Rectangle to = associationPurchaseAnalysis.conversion(associationPurchaseAnalysis.getOriginBounds(),associationPurchaseAnalysis.getRefImageArea());
            targetWindow.drag(from.x, from.y, to.x, to.y);
            analysis();
        }
    }

    public void mouseAloofWindow(Rectangle rect) {
        if (associationPurchaseAnalysis.mouseInRect(rect)) {
            Point safePoint = associationPurchaseAnalysis.findLargestRemainingAreaCenter(rect);
            targetWindow.moveTo(safePoint.x, safePoint.y);
        }
    }

    @Override
    protected void doExecute() throws Exception {
        analysis();
        if (!associationPurchaseAnalysis.isOpened()) return;
        reSetWindow();
        while (!jumpToPage()){
            targetWindow.execute(WindowOperation.delay(500));
        }
        Rectangle petArea = associationPurchaseAnalysis.getPetArea(association.getItemName());
        if (petArea != null) {
            targetWindow.actions().clickIn(petArea).delay(300).clickIn(associationPurchaseAnalysis.getBuyButtonArea()).delay(300);
            okBuy = true;
        }
        close();
    }

    public boolean jumpToPage(){
        int choosePage = associationPurchaseAnalysis.getChooseNumber();
        if (choosePage == association.getPage()) return true;
        else {
            Rectangle rectangle = associationPurchaseAnalysis.getPageButton(association.getPage());
            targetWindow.actions().clickIn(rectangle).delay(300);
            mouseAloofWindow(associationPurchaseAnalysis.windowArea);
            targetWindow.flushScreenshot();
            analysis();
        }
        return false;
    }

}
